<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 头部区域 -->
        <tree-tools
          :tree-data="company"
          @openDialog="openDialog"
          :is-root="true"
        />
        <!-- 树组件区域 -->
        <el-tree :data="data" :props="{ label: 'name', children: 'children' }">
          <tree-tools
            slot-scope="{ data }"
            :tree-data="data"
            @updateList="initData"
            @openDialog="openDialog"
            @editDialog="editDialog"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 新增和修改的弹框 -->
    <add-dpet ref="addDept" @updateList="initData" />
  </div>
</template>

<script>
import { getDepartmentListApi } from "@/api/department";
import TreeTools from "./components/tree-tools.vue";
import addDpet from "./components/add-dept.vue";
import { tranListToTreeData } from "@/utils";
export default {
  name: "Departments",
  data() {
    return {
      company: { name: "传智教育", manager: "负责人", id: "" },
      data: [],
    };
  },
  created() {
    this.initData();
  },
  methods: {
    // 获取数据
    async initData() {
      let { companyName, depts } = await getDepartmentListApi();
      this.companyName = companyName;
      this.data = tranListToTreeData(depts, "");
    },
    // 打开弹框
    openDialog(val) {
      console.log(val);
      this.$refs.addDept.dialogShow = true;
      this.$refs.addDept.formData.pid = val;
    },
    editDialog(val) {
      this.$refs.addDept.getFormDataInfo(val)
    }
  },
  components: {
    TreeTools,
    addDpet,
  },
};
</script>

<style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
